﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product.DAO;
using ERP_RORZE_DLL.Product.database;
using ERP_RORZE_PERMISSION.Base;
using ERP_RORZE_TOOL.DBAccess;

namespace ERP_RORZE_PRODUCT.Product.PartData
{
    public partial class frm_PartEdit : DevExpress.XtraEditors.XtraForm
    {
        ClsPartDataDAO clspartDAO = new ClsPartDataDAO();
        DataTable dt;
        clsPart obj = new clsPart();
        public event EventHandler Reload;

        public string PartNo;
        public string PartName;
        public string CustomerID;
        public string SupplierID;
        private LogUtils log;
        public string UpQty;

        public frm_PartEdit()
        {
            InitializeComponent();
            log = new LogUtils();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (txtPartID.Text.Trim() == "")
            {
                MessageBox.Show("Chưa nhập Part Number");
            }
            else if (txtCustomerID.Text.Trim() == "")
            {
                MessageBox.Show("Chưa nhập CustomerID");
            }
            else if (txtSupplierID.Text.Trim() == "")
            {
                MessageBox.Show("Chưa nhập SupplierID");
            }
            else
            {
                try
                {

                    int.Parse(txtUpQty.Text);
                    obj.customerID = txtCustomerID.Text;
                    obj.PartID = txtPartID.Text;
                    obj.partName = txtPartName.Text;
                    obj.supplierID = txtSupplierID.Text;
                    obj.upQty = int.Parse(txtUpQty.Text);

                    if (clspartDAO.Update(obj) > 0)
                    {

                        //write Log
                        log.Table = LogConstant.Table.Product.PartList;
                        log.Task = LogConstant.Task.Update;
                        log.Description = "PartID: " + obj.PartID;
                        log.WriteLog();

                        MessageBox.Show("Đã cập nhật xong");
                    }
                    else
                    {
                        MessageBox.Show("Xảy ra lỗi khi sửa dữ liệu. Hãy thử lại.");
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void frm_PartEdit_Load(object sender, EventArgs e)
        {
            txtCustomerID.Text = CustomerID;
            txtPartID.Text = PartNo;
            txtPartName.Text = PartName;
            txtSupplierID.Text = SupplierID;
            txtUpQty.Text = UpQty;
        }
    }
}